<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="200px">
      <el-form-item label="计划子单号" prop="planDetailId">
        <el-input
          v-model="queryParams.planDetailId"
          placeholder="请输入计划子单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划主单号" prop="procurePlanId">
        <el-input
          v-model="queryParams.procurePlanId"
          placeholder="请输入计划主单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单价" prop="unitPrice">
        <el-input
          v-model="queryParams.unitPrice"
          placeholder="请输入单价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购数量" prop="procureQuantity">
        <el-input
          v-model="queryParams.procureQuantity"
          placeholder="请输入采购数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预算金额" prop="budgetAmount">
        <el-input
          v-model="queryParams.budgetAmount"
          placeholder="请输入预算金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        任务状态：
        <el-radio-group v-model="queryParams.procureTastStatus" size="mini" @change="handleQuery">
          <el-radio-button label="" >全部</el-radio-button>
          <el-radio-button label="1">已采购完成</el-radio-button>
          <el-radio-button label="2">未采购完成</el-radio-button>
          <el-radio-button label="3">已暂停任务</el-radio-button>
        </el-radio-group>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="multiple"
          @click="handleAdd"
          v-hasPermi="['asset:task:add']"
        >发起采购申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['asset:task:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tastList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="主单号">
              <span>{{ scope.row.procurePlanId }}</span>
            </el-form-item>
            <el-form-item label="子单号">
              <span>{{ scope.row.planDetailId }}</span>
            </el-form-item>
            <el-form-item label="产品名称">
              <span>{{ scope.row.productName }}</span>
            </el-form-item>
            <el-form-item label="产品类型">
              <span>{{ scope.row.productCategory }}</span>
            </el-form-item>
            <el-form-item label="规格型号">
              <span>{{ scope.row.specificationModle }}</span>
            </el-form-item>
            <el-form-item label="计量单位">
              <span>{{ scope.row.measurementUnit }}</span>
            </el-form-item>
            <el-form-item label="单价">
              <span>{{ scope.row.unitPrice }}（元）</span>
            </el-form-item>
            <el-form-item label="采购数量">
              <span>{{ scope.row.procureQuantity }}</span>
            </el-form-item>
            <el-form-item label="预算金额">
              <span>{{ scope.row.budgetAmount }}（元）</span>
            </el-form-item>
            <el-form-item label="已申请数量">
              <span>{{ scope.row.appliedQuantity }}</span>
            </el-form-item>
            <el-form-item label="未申请数量">
              <span>{{ scope.row.unappliedQuantity }}</span>
            </el-form-item>
            <el-form-item label="采购用途">
              <dict-tag :options="dict.type.a_procure_purpose" :value="scope.row.procurePurpose"/>
            </el-form-item>
            <el-form-item label="任务进度">
              <dict-tag :options="dict.type.fa_plan_tast_status" :value="scope.row.procureTastStatus"/>
            </el-form-item>
            <el-form-item label="产品线">
              <span>{{ scope.row.productLineId }}</span>
            </el-form-item>
            <el-form-item label="备注">
              <span>{{ scope.row.remarks }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务进度" align="center" prop="procureTastStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fa_plan_tast_status" :value="scope.row.procureTastStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="主单号" align="center" prop="procurePlanId"  width="150" />
      <el-table-column label="子单号" align="center" prop="planDetailId"  width="150" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品类型" align="center" prop="productCategory" min-width="150" :show-overflow-tooltip="true" />
      <el-table-column label="规格型号" prop="specificationModle"  align="center"/>
      <el-table-column label="计量单位" prop="measurementUnit" align="center"/>
      <el-table-column label="单价（元）" prop="unitPrice" align="center"/>
      <el-table-column label="采购数量" prop="procureQuantity" align="center"/>
      <el-table-column label="预算金额（元）" prop="budgetAmount"  align="center" width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['asset:task:edit']"
            v-if="scope.row.procureTastStatus==='2'">暂停任务</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['asset:task:edit']"
            v-if="scope.row.procureTastStatus==='3'">启动任务</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['asset:task:remove']"
            v-if="scope.row.productName===null || scope.row.productName===''"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listTask,getTask} from "@/api/asset/tast";

export default {
  name: "Tast",
  dicts: ['a_procure_purpose','fa_plan_tast_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的任务进度类型
      taskType:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购计划明细表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planDetailId: null,
        procurePlanId: null,
        productName: null,
        unitPrice: null,
        procureQuantity: null,
        budgetAmount: null,
        procureTastStatus: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购计划明细列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.tastList = response.rows;
        console.log(this.tastList)
        for (let i = 0; i < this.tastList.length; i++) {
          this.tastList[i].productCategory = this.tastList[i].largeCategory+"/"+this.tastList[i].middleCategory+"/"+this.tastList[i].smallCategory;
        }
        this.total = response.total;

        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planDetailId: null,
        procurePlanId: null,
        largeCategoryCode: null,
        middleCategoryCode: null,
        smallCategoryCode: null,
        productName: null,
        specificationModle: null,
        measurementUnit: null,
        unitPrice: null,
        procureQuantity: null,
        budgetAmount: null,
        procurePurpose: null,
        productLineId: null,
        remarks: null,
        appliedQuantity: null,
        unappliedQuantity: null,
        procureTastStatus: null,
        updateUserId: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planDetailId)
      this.taskType = selection.map(item => item.procureTastStatus)
      if(!this.taskType.includes('1') && !this.taskType.includes('3')){
        this.multiple = !selection.length
      }else{
        this.multiple = true;
      }
    },
    /** 发起采购申请按钮操作 */
    handleAdd() {
      const ids = this.ids;
      this.$router.push("/initiate/procure/request/" + ids);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('asset/task/export', {
        ...this.queryParams
      }, `tast_${new Date().getTime()}.xlsx`)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planDetailId = row.planDetailId || this.ids
      getTask(planDetailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购计划明细";
      });
    }
  }
};
</script>
<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 100px;
  left: 10px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-left: 0;
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
